import { Controller, Get, Post, Put, Delete, Body, Param, UseGuards, Request, ForbiddenException } from '@nestjs/common';
import { AgentsService } from './agents.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../auth/roles.guard';
import { Roles, UserRole } from '../auth/roles.decorator';
import { pick } from 'lodash';

const agentInfo = [
  'name',
  'contact',
  'address',
  'phone',
  'type',
  'createTime',
  'source'
];
@Controller('agents')
@UseGuards(JwtAuthGuard)
export class AgentsController {
  constructor(private readonly agentsService: AgentsService) {}

  @Get()
  @UseGuards(RolesGuard)
  @Roles(UserRole.MANAGER)
  findAll() {
    return this.agentsService.findAll();
  }

  // 公共池
  @Get('public')
  async getPublicAgents() {
    return this.agentsService.findUnassigned();
  }

  // 已分配
  @Get('assigned')
  async getAssignedAgents(@Request() req) {
    return this.agentsService.findAssigned(JSON.parse(req.cookies.user));
  }

  @Get('my')
  findMyAgents(@Request() req) {
    return this.agentsService.findMyAgents(req.user.id);
  }

  @Get(':id')
  async findOne(@Param('id') id: string, @Request() req) {
    const agent = await this.agentsService.findOne(+id);
    if (req.user.role !== UserRole.MANAGER && 
        agent.assignedTo !== req.user.id) {
      throw new ForbiddenException('您没有权限查看此代理商信息');
    }
    return agent;
  }

  @Post('create')
  // @UseGuards(RolesGuard)
  // @Roles(UserRole.MANAGER, UserRole.LEADER)
  async create(@Body() createAgentDto: any) {
    return await this.agentsService.create(createAgentDto);
  }

  @Put('update/:agentId')
  async update(@Param('agentId') id: string, @Body() updateAgentDto: any, @Request() req) {
    const info = pick(updateAgentDto, agentInfo);
    return this.agentsService.update(+id, info);
  }

  @Delete('/delete/:agentId')
  @UseGuards(RolesGuard)
  @Roles(UserRole.MANAGER)
  remove(@Param('agentId') id: string) {
    return this.agentsService.remove(+id);
  }

  @Post('/assign/:agentId')
  // @UseGuards(RolesGuard)
  // @Roles(UserRole.MANAGER, UserRole.LEADER)
  async assign(@Param('agentId') id: number, @Body('userId') userId: number, @Request() req) {
    // if (req.user.role === UserRole.LEADER) {
    //   const isSubordinate = await this.agentsService.isSubordinate(userId, req.user.id);
    //   if (!isSubordinate) {
    //     throw new ForbiddenException('您只能将代理商分配给自己的下属');
    //   }
    // }
    return this.agentsService.assign(+id, userId);
  }

  @Post('/claim/:agentId')
  // @UseGuards(RolesGuard)
  // @Roles(UserRole.MANAGER, UserRole.LEADER)
  async claim(@Param('agentId') id: number, @Body('userId') userId: number, @Request() req) {
    return this.agentsService.assign(+id, userId);
  }

  @Post('/return/:agentId')
  async returnToPool(@Param('agentId') id: string, @Request() req) {
    return this.agentsService.returnToPool(+id);
  }

  @Post('/follow-ups/:id')
  async addFollowUp(
    @Param('id') id: string,
    @Request() req,
    @Body() updateInfo: any,
  ) {
    const user = JSON.parse(req.cookies.user);
    return this.agentsService.addFollowUp(+id, user.id, updateInfo);
  }

  @Put('/follow-ups/update/:Id')
  async updateFollowUp(@Param('Id') id: string, @Body() updateInfo: any, @Request() req) {
    const user = JSON.parse(req.cookies.user);
    return this.agentsService.updateFollowUp(+id, user.id, updateInfo);
  }

  @Get('/follow-ups/:id')
  async getFollowUps(@Param('id') id: string, @Request() req) {
    return this.agentsService.getFollowUps(+id);
  }

  @Delete('/follow-ups/:id')
  async deleteFollowUps(@Param('id') id: string, @Request() req) {
    return this.agentsService.deleteFollowUps(+id);
  }

  @Post('/follow-ups/approval/:id')
  async addFollowUpApproval(
    @Param('id') id: string,
    @Request() req,
    @Body() updateInfo: any,
  ) {
    const user = JSON.parse(req.cookies.user);
    return this.agentsService.addFollowUpApproval(+id, user.id, updateInfo);
  }
} 